﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Mechanics.GameEventMechanics {
    import Mechanics.GameMechanicInterfaces.*;
    import Mechanics.*;
    import Classes.*;
    import Classes.util.*;
    import Classes.inventory.*;
    import Mechanics.Transactions.*;

    public class StorageMechanicImplementation extends BaseInventory implements IStorageMechanic {

        private var m_owner:MechanicMapResource;
        private var m_config:MechanicConfigData;
        private var m_sendTransactions:Boolean;
        private var m_restrictedKeywords:Array;

        public function StorageMechanicImplementation(){
            this.m_sendTransactions = true;
            super(null);
        }
        public function hasOverrideForGameAction(_arg1:String):Boolean{
            return ((this[_arg1] is Function));
        }
        public function executeOverrideForGameEvent(_arg1:String, _arg2:Array=null):MechanicActionResult{
            var _local3:MechanicActionResult = new MechanicActionResult(false, true);
            if ((this[_arg1] is Function)){
                _local3 = this[_arg1].apply(this, _arg2);
            };
            return (_local3);
        }
        override protected function loadObject(_arg1:Object):void{
            var _local2:Object = {storage:_arg1};
            super.loadObject(_local2);
        }
        public function initialize(_arg1:IMechanicUser, _arg2:MechanicConfigData):void{
            this.m_owner = (_arg1 as MechanicMapResource);
            this.m_config = _arg2;
            this.loadObject(this.m_owner.getDataForMechanic(this.m_config.type));
            this.m_restrictedKeywords = (this.m_config.params["restrictByKeywords"] as String).split(",");
        }
        public function set sendTransactions(_arg1:Boolean):void{
            this.m_sendTransactions = _arg1;
        }
        override protected function onDataChange(_arg1:String):void{
            this.applyChanges();
        }
        public function applyChanges():void{
            var _local2:String;
            var _local3:int;
            var _local1:Object = {};
            if (((m_storage) && (_local1))){
                for (_local2 in m_storage) {
                    _local3 = int(m_storage[_local2]);
                    _local1[_local2] = _local3;
                };
            };
            this.m_owner.setDataForMechanic(this.m_config.type, _local1);
        }
        public function isVisitEnabled():Boolean{
            return (false);
        }
        override public function isValidName(_arg1:String):Boolean{
            var _local3:String;
            var _local2:Item = Global.gameSettings.getItemByName(_arg1);
            if (_local2){
                for each (_local3 in this.m_restrictedKeywords) {
                    if (_local2.itemHasKeyword(_local3)){
                        return (true);
                    };
                };
            };
            return (false);
        }
        override public function getCapacity(_arg1:String):int{
            var _local2:int;
            var _local3:Item = Global.gameSettings.getItemByName(_arg1);
            if (((_local3) && (_local3.inventoryLimit))){
                _local2 = _local3.inventoryLimit;
            };
            return (_local2);
        }
        public function get restrictedKeywords():Array{
            return (this.m_restrictedKeywords);
        }
        override public function add(_arg1:String, _arg2:int):Boolean{
            var _local3:Boolean = super.add(_arg1, _arg2);
            if (_local3){
                this.sendTransaction("add", _arg1, _arg2);
            };
            return (_local3);
        }
        override public function clear():Boolean{
            var _local1:Boolean = super.clear();
            if (_local1){
                this.sendTransaction("clear");
            };
            return (_local1);
        }
        override public function remove(_arg1:String, _arg2:int):Boolean{
            var _local3:Boolean = super.remove(_arg1, _arg2);
            if (_local3){
                this.sendTransaction("remove", _arg1, _arg2);
            };
            return (_local3);
        }
        public function purchase(_arg1:String, _arg2:int, _arg3:String):Boolean{
            var _local4:Boolean;
            var _local5:Item = Global.gameSettings.getItemByName(_arg1);
            if (((_local5) && (Global.player.canBuyCash((_local5.cash * _arg2), true)))){
                Global.player.cash = (Global.player.cash - (_local5.cash * _arg2));
                _local4 = super.add(_arg1, _arg2);
            };
            if (_local4){
                this.sendTransaction("purchase", _arg1, _arg2, _arg3);
            };
            return (_local4);
        }
        protected function sendTransaction(_arg1:String, ... _args):void{
            if (this.m_sendTransactions){
                GameTransactionManager.addTransaction(new TMechanicAction(this.m_owner, this.m_config.type, MechanicManager.ALL, {
                    operation:_arg1,
                    args:_args
                }));
            };
        }

    }
}//package Mechanics.GameEventMechanics 
